package br.com.felix.dbm.controller;

import java.util.HashMap;
import java.util.Map;

import br.com.felix.dbm.database.Column;
import br.com.felix.dbm.database.DataModel;
import br.com.felix.dbm.database.Table;
import br.com.felix.dbm.ui.TableUi;

/**
 * Controller for a Data Model.
 * */
public class DataModelController {

	/**
	 * Keeps track of all the table controllers of the data model.
	 * */
	private Map<Table,TableController> mapTableControllers = new HashMap<Table,TableController>();


	/**
	 * The dataModel this controller refers to.
	 * */
	private DataModel dataModel;
	
	
	public DataModelController(DataModel dm){
		this.dataModel = dm;
	}
	

	/**
	 * Sends a message to all the table views to redraw themselves.
	 * */
	public void updateTableViews() {
		for (TableController tableController : mapTableControllers.values()) {
			TableUi view = tableController.getView();
			if(view == null){
				throw new NullPointerException("Table controller has no view!");
			}
			view.updateView();
		}
	}



	public void addTableController(TableController tc) {
		mapTableControllers.put(tc.getModel() ,tc);
	}



	/**
	 * Deletes a column from the entire Data Model.
	 * */
	public void deleteColumn(Column column) {
		for (TableController tableController : mapTableControllers.values()) {
			tableController.deleteColumn(column);
		}
	}



	public TableController getTableControllerOf(Table table) {
		return mapTableControllers.get(table);
	}


	public DataModel getDataModel() {
		return dataModel;
	}


	public void clear() {
		mapTableControllers.clear();
		ColumnController.clear();
	}


	public void removeAll(TableControllerSet tableControllerSet) {
		for(TableController tc:tableControllerSet){
			mapTableControllers.remove(tc);
		}
	}
}
